package com.briup.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.briup.cms.domain.Category;
import com.briup.cms.dao.CategoryDao;
import com.briup.cms.domain.vo.CategoryVo;
import com.briup.cms.exception.ServiceException;
import com.briup.cms.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.cms.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author briup
 * @since 2023-03-09
 */
@Service
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    CategoryDao categoryDao;

    @Override
    public void saveOrUpdate(Category category) {
        //判断传入的category是否为空
        if(category == null)
            throw new ServiceException(ResultCode.PARAM_IS_BLANK);
        //判断栏目名称是否重复
        if(category.getName()!=null){
            LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper();
            wrapper.eq(Category::getName,category.getName());
            Category category1 = categoryDao.selectOne(wrapper);
            if(category1!=null){
                throw new ServiceException(ResultCode.DATA_EXISTED);
            }
        }

        if(category.getId()==null){
            //新增操作
            if(category.getName()==null)
                throw new ServiceException(ResultCode.PARAM_NOT_COMPLETE);
            //查询当前最大的order_num
            Integer maxOrder = categoryDao.selectMaxOrder();
            category.setOrderNum(maxOrder+1);
            //如果说传入父栏目id则为二级栏目
            if(category.getParentId()!=null){
                //当前新增的数据为二级栏目
                Category category1 = categoryDao.selectById(category.getParentId());
                if(category1==null){
                    throw new ServiceException(ResultCode.DATA_NONE);
                }
            }
            categoryDao.insert(category);
        }else{
            categoryDao.updateById(category);
        }

    }

    @Override
    public void deleteById(Long id) {
        //判断有没有父id
        Category category = categoryDao.selectById(id);
        if(category == null)
            throw new ServiceException(ResultCode.DATA_NONE);
        if(category.getParentId()==null){
            //说明现在删除的是一个父栏目
            LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Category::getParentId,category.getId());
            Integer count = categoryDao.selectCount(wrapper);
            if(count>0)
                throw new ServiceException(ResultCode.SPECIFIED_QUESTIONED_USER_NOT_EXIST);
        }
        categoryDao.deleteById(id);
    }

    @Override
    public IPage<Category> queryAll(int pageNum, int pageSize) {
        Page<Category> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper();
        wrapper.orderByAsc(Category::getOrderNum);
        categoryDao.selectPage(page,wrapper);
        return page;
    }

    @Override
    public List<CategoryVo> getCategoryVos() {
        return categoryDao.getCategoryVos();
    }
}
